﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for DrugsDictionary.xaml
    /// </summary>
    public partial class DrugsDictionaryPopup : Window
    {
        public DrugsDictionaryPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            SetLists();
        }

        private ObservableCollection<DTCDrug> drugs;
        private ObservableCollection<DTCIssueForm> issueForms;
        private ObservableCollection<DTCBox> boxes;
        private ObservableCollection<DTCManufacturer> manufacturers;
        private ObservableCollection<DTCSupplier> suppliers;

        private void SetLists()
        {
            drugs = new ObservableCollection<DTCDrug>(App.DBI.GetAllDrugs());
            issueForms = new ObservableCollection<DTCIssueForm>(App.DBI.GetAllIssueForms());
            boxes = new ObservableCollection<DTCBox>(App.DBI.GetAllBoxes());
            manufacturers = new ObservableCollection<DTCManufacturer>(App.DBI.GetAllManufacturers());
            suppliers = new ObservableCollection<DTCSupplier>(App.DBI.GetAllSuppliers());

            DrugsListBox.ItemsSource = drugs;
            IssueFormsListBox.ItemsSource = issueForms;
            BoxesListBox.ItemsSource = boxes;
            ManufacturersListBox.ItemsSource = manufacturers;
            SuppliersListBox.ItemsSource = suppliers;
        }

        private void AddDrugButton_Click(object sender, RoutedEventArgs e)
        {
            DrugPopup dp = new DrugPopup();
            dp.Owner = this;
            if (dp.ShowDialog() == true)
            {
                drugs.Add(dp.Drug);
            }
        }

        private void ChangeDrugButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DrugPopup drugPopup = new DrugPopup();
            drugPopup.Drug = (DTCDrug)cmd.Tag;
            drugPopup.ShowDialog();
            DrugsListBox.Items.Refresh();
        }

        private void DeleteDrugButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCDrug drug = (DTCDrug)cmd.Tag;
            if (drug.CanBeDeleted)
            {
                ConfirmationPopup cp = new ConfirmationPopup();
                cp.Message = string.Format("Вы уверенны в том, что хотите удалить препарт {0}, включая все его формы выпуска?", drug.Name);
                if(cp.ShowDialog()== true)
                {
                    drugs.Remove(drug);
                    drug.Delete();
                }
            }
            else
            {
                WarningPopup wp = new WarningPopup();
                wp.Message =
                    string.Format("Препарат {0} не может быть удален, так как он используется в назначениях и заказах", drug.Name);
                wp.Show();
            }
        }

        private void AddIssueFormButton_Click(object sender, RoutedEventArgs e)
        {
            IssueFormPopup ifp = new IssueFormPopup();
            ifp.Owner = this;
            if (ifp.ShowDialog() == true)
            {
                issueForms.Add(ifp.IssueForm);
            }
        }

        private void ViewIssueFormButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            IssueFormPopup ifp = new IssueFormPopup();
            ifp.Owner = this;
            ifp.IssueForm = (DTCIssueForm)cmd.Tag;
            ifp.ShowDialog();
            IssueFormsListBox.Items.Refresh();
        }
        private void DeleteIssueFormButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCIssueForm issueForm = (DTCIssueForm)cmd.Tag;
            if (issueForm.CanBeDeleted)
            {
                ConfirmationPopup cp = new ConfirmationPopup();
                cp.Message = string.Format("Вы уверенны в том, что хотите удалить форму выпуска {0}", issueForm.Name);
                if (cp.ShowDialog() == true)
                {
                    issueForms.Remove(issueForm);
                    issueForm.Delete();
                }
            }
            else
            {
                WarningPopup wp = new WarningPopup();
                wp.Message =
                    string.Format("Форма выпуска {0} не может быть удалена, так как он используется в назначениях и заказах", issueForm.Name);
                wp.Show();
            }
        }

        private void AddBoxButton_Click(object sender, RoutedEventArgs e)
        {
			BoxPopup bp = new BoxPopup();
			bp.Owner = this;
            if (bp.ShowDialog() == true)
            {
                boxes.Add(bp.Box);
            }
        }

        private void ViewBoxButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            BoxPopup bp = new BoxPopup();
            bp.Owner = this;
            bp.Box = (DTCBox)cmd.Tag;
            bp.ShowDialog();
            BoxesListBox.Items.Refresh();
        }

        private void DeleteBoxButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCBox box = (DTCBox)cmd.Tag;
            if (box.CanBeDeleted)
            {
                ConfirmationPopup cp = new ConfirmationPopup();
                cp.Message =
                    string.Format(
                        "Вы уверенны в том, что хотите удалить упаковку {0}",
                        box.FullName);
                if (cp.ShowDialog() == true)
                {
                    boxes.Remove(box);
                    box.Delete();
                }
            }
            else
            {
                WarningPopup wp = new WarningPopup();
                wp.Message =
                    string.Format("Упаковка {0} не может быть удалена, так как она используется в назначениях и заказах", box.FullName);
                wp.Show();
            }
        }

        private void AddManufacturerButton_Click(object sender, RoutedEventArgs e)
        {
            ManufacturerPopup mp = new ManufacturerPopup();
            mp.Owner = this;
            if (mp.ShowDialog() == true)
            {
                manufacturers.Add(mp.Manufacturer);
            }
        }

        private void DeleteManufacturerButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCManufacturer manufacturer = (DTCManufacturer)cmd.Tag;
            manufacturer.Delete();
            manufacturers.Remove(manufacturer);
        }

        private void ViewManufacturerButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            ManufacturerPopup mp = new ManufacturerPopup();
            mp.Manufacturer = (DTCManufacturer)cmd.Tag;
            mp.Owner = this;
            mp.ShowDialog();
            ManufacturersListBox.Items.Refresh();
        }

        private void AddSupplierButton_Click(object sender, RoutedEventArgs e)
        {
            SupplierPopup sp = new SupplierPopup();
            sp.Owner = this;
            if (sp.ShowDialog() == true)
            {
                suppliers.Add(sp.Supplier); 
            }
        }

        private void DeleteSupplierButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCSupplier supplier = (DTCSupplier)cmd.Tag;
            suppliers.Remove(supplier);
            supplier.Delete();
        }

        private void ViewSupplierButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            SupplierPopup sp = new SupplierPopup();
            sp.Owner = this;
            sp.Supplier = (DTCSupplier)cmd.Tag;
            sp.ShowDialog();
            SuppliersListBox.Items.Refresh();
        }

        private void DrugsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListCollectionView view = (ListCollectionView)
                    CollectionViewSource.GetDefaultView(BoxesListBox.ItemsSource);
            view.Filter = new Predicate<object>(FilterRecs);
        }

        private bool FilterRecs(Object item)
        {
            if (item == null)
                return false;
            if (item is DTCBox)
            {
                if (((DTCBox)item).Drug == (DTCDrug)DrugsListBox.SelectedItem)
                    return true;
            }
            return false;
        }
    }

    public class UnitMesaureConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string name;
            name = values[0] + "  " + values[1];
            return name;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            string[] splitValues = ((string)value).Split(' ');
            return splitValues;
        }
    }

}
